home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gcctest / tests03.zoo / trdwr.c < prev    next >
C/C++ Source or Header  |  1992-04-27  |  3KB  |  144 lines

  1. /* Copyright (C) 1991 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include "ansidecl.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <errno.h>
  24.  
  25. #ifndef FILENAME_MAX
  26. #define FILENAME_MAX 256
  27. #endif
  28.  
  29. #ifdef sun
  30. #define remove unlink
  31. #endif
  32.  
  33. extern int errno;
  34.  
  35. int
  36. DEFUN(main, (argc, argv), int argc AND char **argv)
  37. {
  38.   static CONST char hello[] = "Hello, world.\n";
  39.   static CONST char replace[] = "Hewwo, world.\n";
  40.   static CONST size_t replace_from = 2, replace_to = 4;
  41.   char filename[FILENAME_MAX];
  42.   char *name = strrchr(*argv, '/');
  43.   char buf[BUFSIZ];
  44.   FILE *f;
  45.   int lose = 0;
  46.  
  47.   if (name != NULL)
  48.     ++name;
  49.   else
  50.     name = *argv;
  51.  
  52.   (void) sprintf(filename, "test", name);
  53.  
  54. #ifdef atarist
  55.   f = fopen(filename, "wb+");
  56. #else
  57.   f = fopen(filename, "w+");
  58. #endif  
  59.   if (f == NULL)
  60.     {
  61.       perror(filename);
  62.       exit(1);
  63.     }
  64.  
  65.   (void) fputs(hello, f);
  66.   rewind(f);
  67.   (void) fgets(buf, sizeof(buf), f);
  68.   rewind(f);
  69.   (void) fputs(buf, f);
  70.   rewind(f);
  71.   {
  72.     register size_t i;
  73.     for (i = 0; i < replace_from; ++i)
  74.       {
  75.     int c = getc(f);
  76.     if (c == EOF)
  77.       {
  78.         printf("EOF at %u.\n", i);
  79.         lose = 1;
  80.         break;
  81.       }
  82.     else if (c != hello[i])
  83.       {
  84.         printf("Got '%c' instead of '%c' at %u.\n",
  85.            (unsigned char) c, hello[i]);
  86.         lose = 1;
  87.         break;
  88.       }
  89.       }
  90.   }
  91.  
  92.   {
  93.     long int where = ftell(f);
  94.     if (where == replace_from)
  95.       {
  96.     register size_t i;
  97.     for (i = replace_from; i < replace_to; ++i)
  98.       if (putc(replace[i], f) == EOF)
  99.         {
  100.           printf("putc('%c') got %s at %u.\n",
  101.              replace[i], strerror(errno), i);
  102.           lose = 1;
  103.           break;
  104.         }
  105.       }
  106.     else if (where == -1L)
  107.       {
  108.     printf("ftell got %s (should be at %u).\n",
  109.            strerror(errno), replace_from);
  110.     lose = 1;
  111.       }
  112.     else
  113.       {
  114.     printf("ftell returns %u; should be %u.\n", where, replace_from);
  115.     lose = 1;
  116.       }
  117.   }
  118.  
  119.   if (!lose)
  120.     {
  121.       rewind(f);
  122.       if (fgets(buf, sizeof(buf), f) == NULL)
  123.     {
  124.       printf("fgets got %s.\n", strerror(errno));
  125.       lose = 1;
  126.     }
  127.       else if (strcmp(buf, replace))
  128.     {
  129.       printf("Read \"%s\" instead of \"%s\".\n", buf, replace);
  130.       lose = 1;
  131.     }
  132.     }
  133.  
  134.   if (lose)
  135.     printf("Test FAILED!  Losing file is \"%s\".\n", filename);
  136.   else
  137.     {
  138.       (void) remove(filename);
  139.       puts("Test succeeded.");
  140.     }
  141.  
  142.   return(lose ? EXIT_FAILURE : EXIT_SUCCESS);
  143. }
  144.